﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Telerik.WinControls.UI; 
using Telerik.WinControls;

using LIM.model;

namespace LIM.forms.uc_SYS_PhanQuyenHeThong
{
    public partial class uc_SYS_PhanQuyenHeThong_Form : UserControl
    {
        radForm_SYS_PhanQuyenHeThong parentForm = null;

        public uc_SYS_PhanQuyenHeThong_Form()
        {
            InitializeComponent();

        }

        void parentForm_OnIndexChanged()
        {
            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }
        }

        private void uc_SYS_PhanQuyenHeThong_Form_Load(object sender, EventArgs e)
        {
            parentForm = (radForm_SYS_PhanQuyenHeThong)this.ParentForm;
            if (parentForm != null)
            {
                parentForm.OnIndexChanged +=new radForm_SYS_PhanQuyenHeThong.ListControlOnIndexChanged(parentForm_OnIndexChanged);
            }

            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }
        }

        List<FormPermissionsInfo> formPermissionsInfos;
        protected void BindControls()
        {
            formPermissionsInfos = LIMDatas.CreateDatas_FormPermissionsInfo();
            this.radTreeView_DanhSachForms.Nodes.Clear();
            PopulateTreeView_DanhSachForms(0, null);            

            List<tbl_SYS_FormPermissions> formPermissions = parentForm._SYSController.GetList_FormPermissions_ByRowIDRole(parentForm.SelectedEntity.RowID);
            List<tbl_SYS_ControlPermissions> controlPermissions = parentForm._SYSController.GetList_ControlPermissions_ByRowIDRole(parentForm.SelectedEntity.RowID);

            foreach (var item in formPermissionsInfos)
            {
                if (formPermissions.Select(m => m.FormName).Contains(item.Name))
                {
                    item.IsVisibility = true;

                    foreach (var item2 in item.Controls)
                    {
                        tbl_SYS_ControlPermissions tbl_SYS_ControlPermissions= controlPermissions.Where(c => c.FormName == item.Name && c.ControlName == item2.Name).FirstOrDefault();
                        if (tbl_SYS_ControlPermissions != null)
                        {
                            item2.IsVisibility = true;
                            item2.IsEnabled = tbl_SYS_ControlPermissions.IsEnabled.GetValueOrDefault(false);
                        }
                    }
                }
            }



            this.radCheckBox_FromIsVisibility.Checked = false;
            this.radTreeView_DanhSachForms.ClearSelection();
        }

        private void PopulateTreeView_DanhSachForms(int parentId, RadTreeNode parentNode)
        {
            var filteredItems = formPermissionsInfos.Where(item =>
                                        item.ParentID == parentId);

            RadTreeNode childNode;
            foreach (var i in filteredItems.ToList())
            {
                childNode = new RadTreeNode();
                childNode.Text = i.FormName;
                childNode.Value = i.Name;

                if (parentNode == null)
                    this.radTreeView_DanhSachForms.Nodes.Add(childNode);
                else
                    parentNode.Nodes.Add(childNode);

                
                PopulateTreeView_DanhSachForms(i.ID, childNode);
            }
        }

        private void radMenuItem_LuuLai_Click(object sender, EventArgs e)
        {
            this.radGridView_DanhSachControl.EndEdit();

            parentForm._SYSController.Update_FormPermissions_ByRowIDRole(parentForm.SelectedEntity.RowID, formPermissionsInfos);
            
            RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Phân quyền truy cập trên Forms", "Đã lưu thành công!");
        }

        private void radTreeView_DanhSachForms_SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
        {
            //MessageBox.Show(e.Node.Value.ToString());
            FormPermissionsInfo selectedForm = formPermissionsInfos.Find(f => f.Name == this.radTreeView_DanhSachForms.SelectedNode.Value.ToString());
            
            IsNotToggleStateChanged = true;            
            radCheckBox_FromIsVisibility.Checked = selectedForm.IsVisibility;
            radCheckBox_FromIsVisibility.Tag = selectedForm.Name;
            IsNotToggleStateChanged = false;

            this.radGridView_DanhSachControl.DataSource = selectedForm.Controls;
        }

        bool IsNotToggleStateChanged = false;
        private void radCheckBox_FromIsVisibility_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            if (!IsNotToggleStateChanged)
            {
                FormPermissionsInfo selectedForm = formPermissionsInfos.Find(f => f.Name == this.radCheckBox_FromIsVisibility.Tag.ToString());
                selectedForm.IsVisibility = this.radCheckBox_FromIsVisibility.Checked;
            }

            if (this.radCheckBox_FromIsVisibility.Checked)            
                this.radGroupBox_DanhSachControls.Enabled = true;            
            else
                this.radGroupBox_DanhSachControls.Enabled = false;
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }
        }
    }
}
